﻿using GBT.ParamEntity;
using GBT.SystemBusinessCore;
using GBT.SystemEnties;
using NLog;
using QueryDataLogic.Core;
using System;

namespace PersistenceDataLogic.Core
{
    public class OperationAnnounce
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        BasicInfo bi = new BasicInfo();
        DbContext<Announcement> dbContext = null;

        public int OptAnnounce(System_Announcement announcement, ref string msg, ref string number)
        {
            var dbName = bi.GetDBName((int)announcement.DistrictID);
            dbContext = new DbContext<Announcement>(dbName);
            var db = dbContext.Db;
            int result = 0;
            number = DateTime.Now.ToString("ddHHmmssfff");
            try
            {
                db.BeginTran();
                Announcement announcementEntity = new Announcement();
                announcementEntity.Title = announcement.Title;
                announcementEntity.Text = announcement.Text;
                announcementEntity.UserID = (int)announcement.UserID;
                announcementEntity.CreationDate = DateTime.Now;
                announcementEntity.AntDate = DateTime.Now.Date;
                announcementEntity.DistrictID = (int)announcement.DistrictID;
                announcementEntity.IsDelete = false;
                if (announcement.ID == null)
                {
                    announcementEntity.IsCarousel = true;
                    result = dbContext.InsertReturnLineNumber(announcementEntity);
                }
                else if(announcement.ID != null && announcement.ID > 0)
                {
                    announcementEntity.ID = (int)announcement.ID;
                    announcementEntity.IsCarousel = announcement.IsCarousel;
                    announcementEntity.IsDelete = announcement.IsDelete;
                    result = dbContext.UpdateReturnLineNumber(announcementEntity);
                }
                db.CommitTran();
                msg = "新增或修改公告成功";
            }
            catch (Exception ex)
            {
                db.RollbackTran();
                msg = "新增或修改公告失败";
                Console.WriteLine(ex);
                throw ex;
            }
            return result;
        }
    }
}
